package com.gemme.gemme.view;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.gemme.gemme.R;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.setting.GmWebActivity;

import java.util.HashMap;
import java.util.List;

public class ProductIntroduceView extends RelativeLayout {
    private Context context;
    public ProductIntroduceView(Context context) {
        this(context,null);
    }

    public ProductIntroduceView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ProductIntroduceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }
    TextView tv_server;
    TextView tv_address;
    TextView tv_pay;
    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.product_shangping,this);
        tv_server = findViewById(R.id.tv_server);
        tv_address = findViewById(R.id.tv_address);
        tv_pay = findViewById(R.id.tv_pay);

        tv_server.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, GmWebActivity.class);
                intent.putExtra("data","http://47.114.162.17/protocol/index.html?type=protoco2");
                context.startActivity(intent);
            }
        });
    }

    public void initData(long id) {
//        id = 25;
        String url = DataManager.BaseUrl_weicheng + "mall/shopsku/findSomeInfo?productId="
                + id;
        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(getContext()));
//        headMap.put(DataManager.AuthorizationKey,"Bearer 2af9350c-dad4-4e04-b5b9-6a931f9b7b0c");
        DataManager.toServer3(getContext(), url, DataManager.GET_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try{
                    ProIntrduceBean proIntrduceBean = JSON.parseObject(response, ProIntrduceBean.class);
                    List<ProIntrduceBean.DataBean> data = proIntrduceBean.getData();
                    System.out.println();
                    String serviceCommitment = data.get(0).getServiceCommitment();
                    String freightDescription = data.get(0).getFreightDescription();
                    double freight = data.get(0).getFreight();

                    tv_server.setText(serviceCommitment + "");
                    tv_address.setText(freightDescription + "");
                    tv_pay.setText(freight + "");
                }catch (Exception e){
                    //todo 暂时去除空界面显示
//                    setNUllDataView(true);
                }
            }
        });
    }


    static class ProIntrduceBean{

        /**
         * code : 0
         * msg : null
         * data : [{"serviceCommitment":"过敏包退,破损包退,正品保障,进口保税,赠运费险","freightDescription":"会员包邮","freight":8}]
         */

        private int code;
        private Object msg;
        private List<DataBean> data;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public Object getMsg() {
            return msg;
        }

        public void setMsg(Object msg) {
            this.msg = msg;
        }

        public List<DataBean> getData() {
            return data;
        }

        public void setData(List<DataBean> data) {
            this.data = data;
        }

        public static class DataBean {
            /**
             * serviceCommitment : 过敏包退,破损包退,正品保障,进口保税,赠运费险
             * freightDescription : 会员包邮
             * freight : 8.0
             */

            private String serviceCommitment;
            private String freightDescription;
            private double freight;

            public String getServiceCommitment() {
                return serviceCommitment;
            }

            public void setServiceCommitment(String serviceCommitment) {
                this.serviceCommitment = serviceCommitment;
            }

            public String getFreightDescription() {
                return freightDescription;
            }

            public void setFreightDescription(String freightDescription) {
                this.freightDescription = freightDescription;
            }

            public double getFreight() {
                return freight;
            }

            public void setFreight(double freight) {
                this.freight = freight;
            }
        }
    }
}
